package cn.hinglo.gameclaw.data.utils;

import cn.hinglo.gameclaw.data.entity.Result;
import cn.hinglo.gameclaw.user.entity.Goods;
import cn.hinglo.gameclaw.user.entity.Logistics;
import cn.hinglo.gameclaw.user.entity.LogisticsResult;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import lombok.extern.slf4j.Slf4j;

import java.util.HashMap;
import java.util.Map;

/**
 * @createTime: 2018/1/3
 * @author: HingLo
 * @description: 物流查询信息方法
 */
@Slf4j
public class LogisticsUtils {

    private static final String appcode = "65c0c157b8d54d998bd6f5c92dc8e018";
    private static final String HOST = "http://kdwlcxf.market.alicloudapi.com/kdwlcx";
    private static final String RESULT_CODE = "0";
    private static final Map<String, String> HEADER = new HashMap<>();


    static {
        HEADER.put("Authorization", "APPCODE " + appcode);
        HEADER.put("Host", HOST);
    }


    /**
     * 传入物流单号，返回物流信息
     *
     * @param gid 物流订单编号
     * @return
     */
    public static Result getGoodsUtils(String gid) {

        //请求参数
        Map<String, Object> querys = new HashMap<>(1);
        querys.put("no", gid);
        //获取请求的结果
        String body = httpRequestUtils(HOST, HEADER, querys);

        JSONObject jsonObject = JSONUtil.parseObj(body);
        //转化为实体Bean
        Goods goods = JSONUtil.toBean(jsonObject, Goods.class, true);

        if (!RESULT_CODE.equals(goods.getStatus())) {
            return ResultUtils.error("无该快递单号信息,请检测单号是否正确");
        }
        //返回json格式的数据
        return ResultUtils.success(goods.getResult());
    }

    /**
     * 通过快递类型查看名字
     *
     * @param type 快递类型
     * @return 返回快递的名称
     */
    private static String findByTypeUtils(String type) {

        String path = "/express/type";
        //获取response的body
        String body = httpRequestUtils(HOST + path, HEADER, null);
        //解析结果
        JSONObject jsonObject = JSONUtil.parseObj(body);
        //转化为实体bean
        Logistics logistics = JSONUtil.toBean(jsonObject, Logistics.class, true);
        for (LogisticsResult logisticsResult : logistics.getResult()) {
            if (type.equalsIgnoreCase(logisticsResult.getType())) {
                return logisticsResult.getName();
            }
        }

        return "未知快递公司";
    }


    /**
     * 使用 Hutool 中的HttpRequest模块封装请求，返回结果执行结果的字符串
     *
     * @param url    请求URL
     * @param header 请求头
     * @param params 请求参数
     * @return 返回请求结果字符串
     */
    private static String httpRequestUtils(String url, Map<String, String> header, Map<String, Object> params) {
        //设置请求的URL
        HttpRequest httpRequest = HttpRequest.get(url);
        //添加Header
        if (header != null) {
            for (Map.Entry<String, String> entry : header.entrySet()) {
                httpRequest.header(entry.getKey(), entry.getValue());
            }
        }
        //添加参数
        if (params != null) {
            httpRequest.form(params);
        }
        //执行请求
        HttpResponse httpResponse = httpRequest.execute();
        return httpResponse.body();

    }

}
